//
//  NetWorthController.m
//  FinancialProduct
//  净值查询
//  Created by Stella on 2017/8/29.
//  Copyright © 2017年 maq. All rights reserved.
//

#import "HandleNetProduct.h"
#import "NetWorthCell.h"
#import "UIColor+NN.h"
#import "Header.h"
//#import "DBCharts.h"
#import "CKPuslichTool.h"
#import "TransactionInfoCell.h"
#import "NetWorthController.h"
#import "DBHelper.h"
#import "UserModel.h"
#import "RoseModel.h"
#import "MJExtension.h"

#import "XLineChartView.h"

@interface NetWorthController ()
@property (weak, nonatomic) IBOutlet UILabel *chooseNameLa;
@property (weak, nonatomic) IBOutlet UITableView *bottomTable;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UILabel *noDataLabel;
@property (weak, nonatomic) IBOutlet UIView *noDataView;
@property (weak, nonatomic) IBOutlet UILabel *line;
@property (strong,nonatomic)NSArray *nameArray;
@property (weak, nonatomic) IBOutlet UILabel *lineName;
@property (strong,nonatomic)NSArray *dataArray;
@end

@implementation NetWorthController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.chooseNameLa.text=self.SubProductName;
    [self getLineDataWithSubId:self.SubProductId];
}

-(void)getLineDataWithSubId:(NSString *)subId{
    NSDictionary *params=@{@"AAAMethodName":@"InfodisclosureNetSelect",@"SubProductId":subId};
    [HandleNetProduct getListforParams:params Success:^(NSArray *data) {
        /**
         DocDate = "2016/7/29 0:00:00";
         IsSelect = No;
         Maximum = "55.34";
         Minimum = "-16.22";
         Net = "1.1512";
         Rose = "-6.71";
         SumNet = "1.1512";
         SumRose = "15.12";
         **/
        if (data.count>0) {
            self.dataArray=data;
//            [self sortData];
            [self sortMyData];
            [self.bottomTable reloadData];
            self.noDataLabel.text=@"";
            self.noDataView.hidden=YES;
        }else{
//            self.noDataLabel.text=@"暂无数据";
            self.noDataView.hidden=NO;
        }
        
    } Failure:^(NSString *errorInfo) {
        
    }];
}

-(void)sortMyData{
    self.line.hidden=YES;
    self.lineName.hidden=YES;
    XLineChartView *chartView = [[XLineChartView alloc] initWithFrame:self.topView.bounds];
    chartView.xAssistLineColor=[UIColor whiteColor];//纵向辅助线颜色
    chartView.strokeLineWidth=1;//线条宽度
    chartView.coordColor=[UIColor lightGrayColor];
    chartView.coordWidth=0.5;//坐标轴颜色
    chartView.yLabNumber=5;//Y分块个数
    chartView.animationEnable=NO;
    chartView.chartType = XChartLineTypeCurve;//设置绘制类型为曲线
    chartView.showDot = NO;   //设置显示圆点
    chartView.gradientEnable = NO;  //设置启用渐变填充
    chartView.markerLineColor=[UIColor colorWithHexString:@"#BB3535"];
    chartView.scaleType=XChartViewScaleTypeNone;
    NSMutableArray *xTitles = [NSMutableArray array];
    NSMutableArray *yValues = [NSMutableArray array];
    
    for (UIView *vc in self.topView.subviews) {
        [vc removeFromSuperview];
    }
    if (self.dataArray.count==0) {
        return;
    }
    float max=[self.dataArray[0][@"Maximum"] floatValue];
    float min=[self.dataArray[0][@"Minimum"] floatValue];
 
    float sum=max-min;
    if (sum==0) {
        if ([self.dataArray[0][@"Reference"] isEqualToString:@"无"]) {
            self.line.hidden=YES;
            self.lineName.hidden=YES;
        }else{
            self.line.hidden=NO;
            self.lineName.hidden=NO;
            NSString *nameSS=[NSString stringWithFormat:@"%@增长率",self.dataArray[0][@"Reference"]];
            self.lineName.text=nameSS;
        }
        return;
    }
    float sizeH = sum/5;
    //    NSLog(@"sizeH==%d",sizeH);
    NSString *hh1=[NSString stringWithFormat:@"%.2f",min];
    NSString *hh2=[NSString stringWithFormat:@"%.2f",min+sizeH];
    NSString *hh3=[NSString stringWithFormat:@"%.2f",min+sizeH*2];
    NSString *hh4=[NSString stringWithFormat:@"%.2f",min+sizeH*3];
    NSString *hh5=[NSString stringWithFormat:@"%.2f",min+sizeH*4];
    NSString *hh6=[NSString stringWithFormat:@"%.2f",max];
    NSMutableArray *leftdataA = [NSMutableArray new];
    [leftdataA addObject:hh1];
    [leftdataA addObject:hh2];
    [leftdataA addObject:hh3];
    [leftdataA addObject:hh4];
    [leftdataA addObject:hh5];
    [leftdataA addObject:hh6];
    NSMutableArray* chartData = [NSMutableArray arrayWithCapacity:self.dataArray.count];
    NSMutableArray* chartData2 = [NSMutableArray arrayWithCapacity:self.dataArray.count];
    NSMutableArray* chartData3 = [NSMutableArray arrayWithCapacity:self.dataArray.count];
    
    NSArray* reversedArray = [[self.dataArray reverseObjectEnumerator] allObjects];
    
    for (int i=0; i<reversedArray.count; i++) {

        [chartData addObject:reversedArray[i][@"SumRose"]];
        [chartData2 addObject:reversedArray[i][@"Rose"]];
        [xTitles addObject:@""];
        NSDate *dateTime =  [DBHelper dateFromCustomString:reversedArray[i][@"DocDate"] format:@"yyyy/MM/dd HH:mm:ss"];
        NSString *time = [DBHelper stringFromCustomFormatDate:dateTime format:@"yyyy-MM-dd"];
        [chartData3 addObject:time];
    }
    [yValues addObject:chartData];
    
    NSArray *nameTitleArray=[NSArray array];
    NSArray *colorA = [NSArray array];
    if ([self.dataArray[0][@"Reference"] isEqualToString:@"无"]) {
        self.line.hidden=YES;
        self.lineName.hidden=YES;
        colorA=@[[UIColor colorWithHexString:@"#65AFF7"]];
        nameTitleArray=@[@"产品净值增长率"];
    }else{
        self.line.hidden=NO;
        self.lineName.hidden=NO;
        NSString *nameSS=[NSString stringWithFormat:@"%@增长率",self.dataArray[0][@"Reference"]];
        self.lineName.text=nameSS;
        [yValues addObject:chartData2];
        colorA=@[[UIColor colorWithHexString:@"#65AFF7"],[UIColor blackColor]];
        nameTitleArray=@[@"产品净值增长率",nameSS];
    }
    
    
    chartView.chartColors=colorA;
    chartView.xTitles = chartData3;    //设置X
    chartView.yValuesArray = yValues;  //设置Y
    chartView.yNameArr=leftdataA;
    chartView.xDateArrays=chartData3;
    chartView.dataNameArr=nameTitleArray;
    chartView.markerLineColor=[UIColor colorWithHexString:@"#BB3535"];
    chartView.scaleType=XChartViewScaleTypeNone;

    [self.topView addSubview:chartView];
    [chartView strokeChart];  //绘制曲线
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    return 40;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 
    return self.dataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NetWorthCell *cell=[tableView dequeueReusableCellWithIdentifier:@"NetWorthCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryNone;
    
    NSDictionary *info=self.dataArray[indexPath.row];
    RoseModel *mode=[RoseModel mj_objectWithKeyValues:info];
    cell.roseMode=mode;
    return cell;
  
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
 
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}


@end
